package com.shuoyue.ycgk.ui.papergroups.groupcommon;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.papergroups.BasePaperGroupActivity;
import com.shuoyue.ycgk.ui.search.SearchActivity;
import com.shuoyue.ycgk.views.TabLayoutUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PaperGroupCommonActivity extends BasePaperGroupActivity {
    int queryType;
    boolean isMine;
    int indexId = -1;
    @BindView(R.id.iv_right)
    ImageView ivRight;

    public static void start(Context source, int queryType, int indexId, boolean isMine) {
        source.startActivity(new Intent(source, PaperGroupCommonActivity.class)
                .putExtra("queryType", queryType)
                .putExtra("indexId", indexId)
                .putExtra("isMine", isMine));
    }

    public static void start(Context source, int queryType, int indexId,
                             boolean isMine, String typename) {
        source.startActivity(new Intent(source, PaperGroupCommonActivity.class)
                .putExtra("queryType", queryType)
                .putExtra("indexId", indexId)
                .putExtra("typename", typename)
                .putExtra("isMine", isMine));
    }

    @Override
    protected void initView() {
        super.initView();
        queryType = getIntent().getIntExtra("queryType", -1);
        indexId = getIntent().getIntExtra("indexId", -1);
        isMine = getIntent().getBooleanExtra("isMine", false);
        selectBaseType = getIntent().getStringExtra("typename");


        switch (queryType) {
            case 1:
                pageTitle.setText("历年真题");
                break;
            case -1:
                pageTitle.setText("题库");
                break;
            case 2:
                pageTitle.setText("时政热点");
                break;
            case 3:
                pageTitle.setText("自由组卷");
                break;
            case 4:
                pageTitle.setText("我的题集");
                break;
        }
    }

    @Override
    public void setTabType(List<Type> type) {
        int index = 0;
        fragmentMainItems = new ArrayList<>();
        for (Type topType : type) {
            fragmentMainItems.add(FragmentCommonPaper.getInstance(topType, isMine, queryType));
            if (topType.getId() == indexId) {
                index = type.indexOf(topType);
            }
        }
        if (queryType == 1) {
            //真题加搜索
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setImageResource(R.mipmap.search);
            ivRight.setOnClickListener(v -> SearchActivity.start(mContext, 6, topType.getId(), topType.getName()));
        }
        indexFragmentAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(indexFragmentAdapter);
        refreshlayout.setEnableRefresh(false);
        viewPager.setCurrentItem(index);
        TabLayoutUtil tabLayoutUtil = new TabLayoutUtil();
        tabLayoutUtil.setTabTextSizeWhenSelect(tabLayout, type, index);
    }
}
